﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Web;
using System.Web.Mvc;
using SHOP.Areas.Admin.Models;

namespace SHOP.Areas.Admin.Controllers
{
    public class SeoController : Controller
    {
        //
        // GET: /Admin/Seo/

        //Kết nối dữ liệu
        SeoModelDataContext dataTable = new SeoModelDataContext();

        public Constants Constants = new Constants();
        public string NameController = "seo";

        [HandleError]
        public ActionResult Index()
        {
            return View();
        }

        [HandleError, HttpPost]
        public ActionResult LoadContent()
        {
            string Sort_By = "";
            string Sort_Type = "";
            string Search_Type = "title";
            string KeyWord = Request.Form["keyword"];

            string[] Columns = { "title", "keyword", "description", "googleid", "facebookid" };
            string sortby = Array.Find(Columns, delegate(string s) { return s == Request.Form["sort_by"]; });

            string[] Sort = { "asc", "desc" };
            string sorttype = Array.Find(Sort, delegate(string s) { return s == Request.Form["sort_type"]; });

            if (sortby != null) { Sort_By = sortby; } else { Sort_By = "id"; };
            if (sorttype != null) { Sort_Type = sorttype; } else { Sort_Type = "desc"; };

            //Pagination
            var page = int.Parse(Request.Form["page"]);
            if (KeyWord != "")
            {
                var ListItem = (from u in dataTable.seos select u).Where(Search_Type + ".Contains(@0)", KeyWord).OrderBy(Sort_By + " " + Sort_Type).Skip((page - 1) * Constants.pageSize).Take(Constants.pageSize).ToList();
                ViewBag.numPage = (from u in dataTable.seos select u).Where(Search_Type + ".Contains(@0)", KeyWord).Count();
                ViewBag.View = ListItem;

            }
            else
            {
                var ListItem = (from u in dataTable.seos select u).Where("id == @0", 1).OrderBy(Sort_By + " " + Sort_Type).Skip((page - 1) * Constants.pageSize).Take(Constants.pageSize).ToList();
                ViewBag.numPage = (from u in dataTable.seos select u).Where("id != @0", 1).Count();
                ViewBag.View = ListItem;
            }

            ViewBag.Page = page;
            ViewBag.Keyword = KeyWord;
            ViewBag.Sort_By = Sort_By;
            ViewBag.Sort_Type = Sort_Type;
            ViewBag.pageSize = Constants.pageSize;

            return View();
        }

        [HandleError]
        public ActionResult Update()
        {
            if (Request.QueryString["id"] != null && System.Text.RegularExpressions.Regex.IsMatch(Request.QueryString["id"], @"\d"))
            {
                var id = Convert.ToInt32(Request.QueryString["id"]);
                var ListItem = dataTable.seos.SingleOrDefault(b => b.id == id);
                ViewBag.View = ListItem;
            }
            return View();
        }

        [HandleError, HttpPost]
        public ActionResult Save()
        {
            var json = "";

            //Update
            var id = int.Parse(Request.Form["id"]);
            int recCount = dataTable.seos.Count(b => b.id == id);

            if (ModelState.IsValid && recCount > 0)
            {
                var ListItem         = dataTable.seos.Single(b => b.id == id);
                ListItem.title       = Request.Form["title"];
                ListItem.keyword     = Request.Form["keyword"];
                ListItem.description = Request.Form["description"];
                ListItem.facebookid  = Request.Unvalidated["facebookid"];
                ListItem.googleid    = Request.Unvalidated["googleid"];
                dataTable.SubmitChanges();

                json = "success";
            }
            else
            {
                json = "fail";
            }

            return Json(new { st = json });
        }
    }
}
